package hibernate.dao.impl;

import hibernate.HibernateUtil;
import hibernate.dao.PersonDAO;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;

import api.Person;

/**
 * @author Anastasiya Uryadkina
 * date: 19.04.2011	
 * time: 1:04:17
 * project: SimpleServer2rmi
 */
public class PersonDaoImpl implements PersonDAO
{
   public Long addPerson(Person person)
   {
      Session session = null;
      Long id = 0L;
      try
      {
         session = HibernateUtil.getSessionFactory().openSession();
         session.beginTransaction();
         id = (Long) session.save(person);
         session.getTransaction().commit();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      finally
      {
         if (session != null && session.isOpen())
         {
            session.close();
         }
      }
      return id;
   }

   @Override
   public void updatePerson(Person person)
   {
      Session session = null;
      try
      {
         session = HibernateUtil.getSessionFactory().openSession();
         session.beginTransaction();
         session.update(person);
         session.getTransaction().commit();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      finally
      {
         if (session != null && session.isOpen())
         {
            session.close();
         }
      }
   }

   @Override
   public Person getPersonById(Long person_id)
   {
      Session session = null;
      Person person = null;
      try
      {
         session = HibernateUtil.getSessionFactory().openSession();
         person = (Person) session.load(Person.class, person_id);
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      finally
      {
         if (session != null && session.isOpen())
         {
            session.close();
         }
      }
      return person;
   }

   @SuppressWarnings("unchecked")
   @Override
   public List<Person> getAllPersons()
   {
      Session session = null;
      List<Person> persons = new ArrayList<Person>();
      try
      {
         session = HibernateUtil.getSessionFactory().openSession();
         persons = session.createCriteria(Person.class).list();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      finally
      {
         if (session != null && session.isOpen())
         {
            session.close();
         }
      }
      return persons;
   }

   @Override
   public void deletePerson(Person person)
   {
      Session session = null;
      try
      {
         session = HibernateUtil.getSessionFactory().openSession();
         session.beginTransaction();
         session.delete(person);
         session.getTransaction().commit();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      finally
      {
         if (session != null && session.isOpen())
         {
            session.close();
         }
      }
   }
}
